<?
session_start();
include "adm_header.php";
$force=getParam("force","");
ini_set("max_execution_time","1000");
writeCommonAdminTable(LA_ADMIN_TEST,1);
echo "<p class='bg'>".LA_ADMIN_TEST_DESCR."</p>";


if ($set_fill_db!=1)
{	
	echo LA_ADMIN_TEST_DISABLED;
	writeCommonAdminTable(LA_ADMIN_TEST,"");
	include "adm_footer.php";
	exit;
}


if (getParam("del",""))
{
	if (getParam("confirmed","")==1 && WA())
	{

		$sql="select * from $usr_tbl";
		$res=q($sql);
		while ($row=mysql_fetch_array($res))
		{
			echo $row["user_id"] . $row["name"] . "<br>";
			delete_user($row["user_id"]);
		}
		
		$r = "delete from $q_tbl";
		$sql_r = q($r);  
		
		$r = "delete from $banner_tbl";
		$sql_r = q($r);  
		
		$r = "delete from $pay_tbl";
		$sql_r = q($r);  
		
		$r = "delete from $stat_tbl";
		$sql_r = q($r);  
		
		$r = "delete from $template_tbl";
		$sql_r = q($r);  
		
		q("update $cat_tbl set cat_total=0");
		
	}
	else 
	{
		echo "<strong class='red'>".LA_ADMIN_WARNING."</strong><br />";
		echo LA_ADMIN_TEST_WARNING;
		echo "<p><a href='?del=".getParam("del","")."&confirmed=1'><strong>".LA_ADMIN_CONFIRM_YES."</strong></a></p>";
	}
}
@mkdir("$set_path/images",octdec($set_chmod));
$testpath=$set_path."/admin/testing";


if (!is_dir($testpath))
{
	echo LA_ADMIN_TEST_NO_FILES;
}
else 
{
	$dir = opendir($testpath . "/test_images");
	
	while ($file = readdir($dir))
	{
	        if ($file <> "." AND $file <> ".." AND $file <> "")
	        {
	        	$mod = "";
	        	//echo $file . "<br>";
	        	//echo getNameOfLargeImage($file);
	      		$randomFiles[] = $file;
	        	//copy("images/images_for_new/$file","images/$file");  	
		 	    //copy("images/images_for_new/$file","images/".getNameOfLargeImage($file));  
	        }
	        
	}
	closedir($dir);

$num_randomFiles=count($randomFiles);
$timeFuture=addDaysToTimeStamp(rand(0,100),time());

$usernames = file("$testpath/username_list.txt");
$randomNames=split(",",$usernames[0]);
$num_randomNames=count($randomNames);

if (!$set_testdb_num_ads)
	$set_num_items=1500;
else 
	$set_num_items=$set_testdb_num_ads;
	
	

	

$sql="select * from $cat_tbl WHERE cat_allow_ads='on'";
$res=q($sql);
while ($row=mysql_fetch_array($res))
{
	$randomCatList[]=$row["cat_id"];
	
}
$num_randomCat=count($randomCatList);

$handle = fopen($testpath . "/word_list.txt", "r");
while (!feof($handle)) {
   $buffer = fgets($handle, 4096);
   
   $randomSentence[]=$buffer;  

   
}
fclose($handle);
$num_randomSentence=count($randomSentence);

if ($set_image_program==1)
	$lType="ImageMagick";
elseif ($set_image_program==2)
	$lType="GD";
else
	$lType=LA_ADMIN_TEST_IMG_NOTSET;
	
echo "<div style='width:300px;padding:0px;'><table padding='0' cellspacing='0' cellpadding='0'>";
echo "<tr><td>".LA_ADMIN_TEST_FAKE_NAMES."</td><td>".$num_randomFiles . "</td></tr>";
echo "<tr><td>".LA_ADMIN_TEST_NUMCATS."</td><td>".$num_randomCat . "</td></tr>";
echo "<tr><td>".LA_ADMIN_TEST_SENTENCES."</td><td>".$num_randomSentence . "</td></tr>";
echo "<tr><td>".LA_ADMIN_TEST_NUMADS."</td><td><a href='adm_set.php?file=opt_inc.php#set_testdb_num_ads'>".$set_num_items . "</a></td></tr>";
echo "<tr><td>".LA_ADMIN_TEST_IMG_TYPE."</td><td><a href='adm_set.php?file=opt_inc.php#set_image_program'>".$lType . "</a></td></tr>";
echo "</table></div>";

echo "<p><strong><img src='../layout_images/pointer.gif' hspace='3' /><a href='?start=1'>".LA_ADMIN_TEST_START_FILL."</a></strong></p>";
echo "<p><img src='../layout_images/pointer.gif' hspace='3' /><a href='?del=1'>".LA_ADMIN_TEST_EMPTY_DB."</a></p>";


if ($num_randomCat==0)
{
	echo "<p class='red'>" . LA_ADMIN_TEST_MISSING_CATS ."</p>";
}

if (getParam("start","") && WA())
{
	
	while ($counter<$set_num_items)
	{
		$this_user="";
		$email="";
		
		$this_user=trim($randomNames[rand(0,$num_randomNames)]);
		$counter++;
		
		$this_user=strToDb($this_user);
		
		// Find if a user is already added
		$sql="select user_email from $usr_tbl where user_name='$this_user'";
		$res=q($sql);
		$row=mysql_fetch_array($res);
		$email=$row["user_email"];
		
		$timeFuture_1=addDaysToTimeStamp(rand(200,600),time());
		$timePast_1=subtractDaysFromTimeStamp(rand(200,600),time());
		$timePast_2=subtractDaysFromTimeStamp(rand(1,199),time());
		$timeAd=subtractDaysFromTimeStamp(rand(1,90),time());
		
		if (strlen($this_user)>3)
		{
		
		if ($email=="" || !$email)
		{
			$email=eregi_replace(" ","",strtolower((substr($this_user,0,15) . "@noemail.com")));
			$sql="insert into $usr_tbl (user_email,user_name,user_pass_md5,user_date,user_date_last_login) values ('$email','$this_user',md5('$this_user'),$timePast_1,$timePast_2)";
			echo $sql . "<br>";
			$res=q($sql);
			$user_id=mysql_insert_id();
		}
		
		$this_title=strToDb(ucfirst(substr($randomSentence[rand(0,$num_randomSentence)],0,80)));
		$this_descr=strToDb(ucfirst($randomSentence[rand(0,$num_randomSentence)]));

		
		if (strlen($this_title)>3)
		{
			$file=$randomFiles[rand(0,$num_randomFiles)];
			if (trim($file)<>"")
			{
			$randCat=$randomCatList[rand(0,$num_randomCat-1)];
			$sql="insert into $ads_tbl (ad_title,ad_description,ad_date_expire,ad_owner,ad_cat_id,ad_date,ad_has_picture) value ('$this_title','$this_descr',$timeFuture_1,$user_id,$randCat,$timeAd,1)";
			echo $sql . "<br>";
			categoryCounter($randCat,1);
			
			$res=q($sql);			
			$adid=mysql_insert_id();
			
			//$file=$adid . "_" . $file;
			
			$lThumbImage=$adid . "_" . eregi_replace(".jpg","_tmb2.jpg",$file);
			$lSmallImage=$adid . "_" . eregi_replace(".jpg","_tmb1.jpg",$file);
			$lLargeImage=$adid . "_" . $file;	
		
			
	 		if ($set_image_program=="2")
	 		{
	 			$xyArray=split("x",$set_image_size_large);
	 			$x=$xyArray[0];
	 			$y=$xyArray[1];
	 			makeGDThumb("$testpath/test_images/$file","$set_path/images/$lLargeImage","jpg",$x,$y);	
	 			
	 			$xyArray=split("x",$set_image_size_small);
	 			$x=$xyArray[0];
	 			$y=$xyArray[1];
	 			makeGDThumb("$testpath/test_images/$file","$set_path/images/$lSmallImage","jpg",$x,$y);	
	 			
	 			$xyArray=split("x",$set_image_size_thumb);
	 			$x=$xyArray[0];
	 			$y=$xyArray[1];
	 			makeGDThumb("$testpath/test_images/$file","$set_path/images/$lThumbImage","jpg",$x,$y);	
	 			
			
	 		}
	 		else if ($set_image_program=="1")
	 		{	
				makeIMThumb("$testpath/test_images/$file","$set_path/images/$lLargeImage","$set_image_size_large");
				makeIMThumb("$testpath/test_images/$file","$set_path/images/$lSmallImage","$set_image_size_small");
				makeIMThumb("$testpath/test_images/$file","$set_path/images/$lThumbImage","$set_image_size_thumb");
				//makeIMThumb("$testpath/test_images/$file","../images/large_$file","300x300");
	 		}
			$sq = "INSERT INTO $pic_tbl (pic_ad_id,filename,main) VALUES ('$adid','$lThumbImage',1)";
			echo $sq . "<br>";
			userAdsCounter($user_id,1);	
			$result=q($sq);
			//$id = mysql_insert_id();
			
			}
		}
		}
	

	}
	echo "<p><b>$counter</b> was added!</p>";
}
}

writeCommonAdminTable("Database Testing","");
include "adm_footer.php";
?>